import { AstContext } from "../context";
import { RedPacketType } from "../utils/useSetting";
import { AstVisitor } from "../visitor";

export interface RedPacketInfo {
    type: RedPacketType;
    total: number;
    count: number;
    unit: string;
    password: string;
    desc: string;
    sub_title: string;
    title: string;
}

export class RedPacketInfoVisitor extends AstVisitor {
    visitTextView(node: AutoJs.UiObject, ctx: AstContext): boolean {
        const id = node.id()
        const text = node.text()
        if (id) {
            if (["common_red_packet_pop_prepare_reward_desc_view", "red_packet_pop_prepare_source_desc_view", "title_text_view"].some(it => id.endsWith(it))) {
                if (text) {
                    const res = text.match(/主播的(.*?)红包/)
                    if (res && res.length > 0) {
                        const [, type] = res;
                        ctx.set('type', type)
                    }
                    else if (/.*?穿云箭/.test(text)) {
                        ctx.set('type', '穿云箭')
                    }
                    else if (/.*?的快币红包/.test(text)) {
                        ctx.set('type', '观众包')
                    }
                    else if (/.*?礼物红包/.test(text)) {
                        ctx.set('type', '礼物')
                    } else {
                        ctx.set('type', '其他')
                    }
                    ctx.set('title', text)
                }
            }
            // id("red_packet_cover_reward_sub_pack_count")
            if (id.endsWith('red_packet_cover_reward_sub_pack_count') || id.endsWith('live_sec_kill_red_packet_reward_gift_item_tag')) {
                if (text) {
                    ctx.set('total', parseInt(text.replace('x', '')))
                }
            }
            if (id.endsWith('red_packet_cover_reward_usual_count') || id.endsWith('live_sec_kill_red_packet_pop_prepare_reward_kcoin_view')) {
                if (text) {
                    ctx.set('count', parseInt(text))
                }
            }
            if (id.endsWith("red_packet_cover_reward_usual_unit") || id.endsWith('live_sec_kill_prepare_reward_desc_text_view')) {
                if (text) {
                    ctx.set('unit', text)
                }
            }
            if (id.endsWith('prepare_reward_password_text_view')) {
                if (text) {
                    ctx.set('password', text)
                }
            }
            if (id.endsWith('second_line_text_view') || id.endsWith('tips_text_view')) {
                if (text) {
                    ctx.set('desc', text)
                }
            }
            if (id.endsWith('first_line_text_view')) {
                if (text) {
                    ctx.set('sub_title', text)
                }
            }
        }
        return false;
    }
}
export const redPacketInfoVisitor = new RedPacketInfoVisitor()